const path = require('path')
const entryConfig = require('./config/entries')
const HtmlWebpackPlugin = require('html-webpack-plugin')

entries = entryConfig.getEntries()

const entry = {}
const htmlWebpackPlugins = []
entries.map(it=> {
  entry[it.key] = it.path
  htmlWebpackPlugins.push(new HtmlWebpackPlugin({
    filename: it.url,
    template: it.template,
    inject: true,
    chunks: [it.key]
  }))
})

module.exports = {
  entry: entry,
  output: {
    filename: '[name]-[hash].js?',
    path: path.resolve(__dirname, './dist')
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: '/node_modules',
        use: {
          loader: 'babel-loader'
        } 
      },
      {
        test: /\.html$/,
        use: {
          loader: 'html-loader'
        }
      }
    ]
  },
  plugins: [
    ...htmlWebpackPlugins
  ],
  devServer: {
    contentBase: path.join(__dirname, "../dist"), //网站的根目录为 根目录/dist，如果配置不对，会报Cannot GET /错误
    port: 9000, //端口改为9000
    open:true // 自动打开浏览器，适合懒人
  }
}